/*******************************************************************************
 * Copyright (c) 2010 Marcelo Cataldo, Sangeeth Nambiar
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * DepsGraphNode.java
 * 
 * Implements an abstraction layer for a dependency graph node. Each node
 * belongs to one and only one node set (see DepsGraphNodeSet.java)
 * 
 * Contributors:
 *    Marcelo Cataldo  - initial implementation 
 *    Sangeeth Nambiar - initial implementation
 *******************************************************************************/
package edu.cmu.isr.dinvenio.model;

import java.util.*;

public class DepsGraphNode {
	/*
	 * 
	 */
	private String id;            // label of the node
	@SuppressWarnings("unused")
	private String nodeSetName;   // name of the nodeset the node belongs to
	private Hashtable<String,DepsGraphProperty> props;
	
	// Metrics/properties computed for this node
	public double inDegree;
	public double outDegree;
	public double inWeight;
	public double outWeight;
	
	/*
	 * Constructors
	 */
	public DepsGraphNode(String id, String nodeSetName) {
		this.id          = id;
		this.props       = new Hashtable<String,DepsGraphProperty>();
		this.nodeSetName = nodeSetName;

		this.inDegree  = 0;
		this.inWeight  = 0;		
		this.outDegree = 0;
		this.outWeight = 0;
	}
	
	/*
	 * ACCESSOR methods
	 */
	public String getId()
	{
		return id;
	}
	
	public Enumeration<String> getPropertyNames()
	{
		return props.keys();
	}
	
	public String getPropertyType(String pname)
	{
		if (!props.isEmpty()) {
			DepsGraphProperty p = props.get(pname);
			if (p != null) { return p.getType(); }
		}
		return null;
	}
	
	public String getPropertyValue(String pname)
	{
		if (!props.isEmpty()) {
			DepsGraphProperty p = props.get(pname);
			if (p != null) { return p.getValue(); }
		}
		return null;
	}
	
//	public  DepsGraphProperty getDepsGraphProperty(String pname)
//	{
//		return props.get(pname);
//	}
	public  Hashtable<String, DepsGraphProperty> getDepsGraphProperties()
	{
		return props;
	}
	
	public void addProperty(String n, String t, String v)
	{
		DepsGraphProperty p = new DepsGraphProperty(n, t, v);
		props.put(n, p);
	}
	
//	public String getNodeSetName() 
//	{
//		return this.nodeSetName;
//	}
	
	/*
	 * Methods for Visualization
	 */
	public String getToolTip() {		
		String ret = this.id + "\n";		
		Enumeration<String> k = this.props.keys();
		while(k.hasMoreElements()){
			//Object key = k.nextElement();
			ret = ret + "\n" + this.props.get(k.nextElement()).toString();
		}
		return ret;
	}

	/*
	 * Special Methods
	 */
	static public String generateKey(String sName, String nid)
	{
		return new String(sName + "." + nid);
	}
	
	static public String getKeyNodeName(String nid) throws NullPointerException
	{
		int idx = nid.indexOf('.');
		String nname = nid.substring(0, idx);
		if (nname == null) { throw new NullPointerException(); }
		return nname;
	}
	
	static public String getKeyNodeSetName(String nid) throws NullPointerException
	{
		int idx = nid.indexOf('.');
		String nname = nid.substring(idx, nid.length());
		if (nname == null) { throw new NullPointerException(); }
		return nname;
	}	
	
	@Override
	public String toString() {
		return this.id;
	}
}
